'use strict';

const Controller = require('egg').Controller;
const lodash = require('lodash');

class SysTestController extends Controller {
    /**
     * 获取列表
     */
    async querylist() {
        const { ctx } = this;
        const { queryParams,pageSize,page } = ctx.request.body;
        let data = await ctx.service.sysTest.querylist(queryParams,pageSize,page);
        ctx.body = {
            code: 200,
            msg: "成功",
            data: {tabledata:data}
        };
    }

    /**
     * 新增
     */
    async add() {
        const { ctx,app } = this;
        console.log("提交的数据",ctx.request.body);
        let submitBody = ctx.request.body;
        //验证必填项
        await app.utils.validator.required(['username','name','age'],submitBody);
        //过滤非必要项
        let submitData = await app.utils.validator.getSubmitData(['username','name','age','phone','email'],submitBody);
        
    //添加组件值
    let id_uuid = await app.utils.uuid.v1();
    submitData.id = id_uuid;
    
        //操作相关信息
        let userid = await ctx.service.auth.getColonyUserId();
        submitData.create_by = userid;
        submitData.create_time = new Date();
        submitData.update_by = userid;
        submitData.update_time = new Date();
        //保存
        let rs = await ctx.service.sysTest.create(submitData);
        ctx.body = {
            code: 200,
            msg: "保存成功",
            data: {},
        };
    }

    /**
     * 编辑
     */
    async edit() {
        const { ctx,app } = this;
        console.log("提交的数据",ctx.request.body);
        let submitBody = ctx.request.body;
        //验证主键是否存在
        await app.utils.validator.required(['id'],submitBody);
        //过滤非必要项
        let submitData = await app.utils.validator.getSubmitData(['name','age','phone','email'],submitBody);
        //操作相关信息
        let userid = await ctx.service.auth.getColonyUserId();
        submitData.update_by = userid;
        submitData.update_time = new Date();
        //保存
        let rs = await ctx.service.sysTest.update(submitData);
        ctx.body = {
            code: 200,
            msg: "编辑成功",
            data: {},
        };
    }

    /**
     * 删除
     */
    async delete() {
        const { ctx,app } = this;
        console.log("提交的数据",ctx.request.body);
        let submitBody = ctx.request.body;
        //验证主键是否存在
        await app.utils.validator.required(['id'],submitBody);
        //过滤非必要项
        let submitData = await app.utils.validator.getSubmitData(['id'],submitBody);

        //操作相关信息
        //let userid = await ctx.service.auth.getColonyUserId();
        //submitData.update_by = userid;
        //submitData.update_time = new Date();
        //删除
        let rs = await ctx.service.sysTest.delete(submitData.id);
        ctx.body = {
            code: 200,
            msg: "删除成功",
            data: {},
        };
    }

    /**
     * 通过主键查询
     */
    async querybyid() {
        const { ctx,app } = this;
        console.log("提交的数据",ctx.request.body);
        let {id} = ctx.request.body;
        
        //查询
        let rs = await ctx.service.sysTest.queryById(id);
        ctx.body = {
            code: 200,
            msg: "成功",
            data: rs,
        };
    }

}

module.exports = SysTestController;